﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 找质数
{
    class Program
    {
        /// <summary>
        /// 输出质数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //开始时间
            DateTime starttime = DateTime.Now;
            //寻找十万以内的质数
            //计数和十行输出
            int k = 0;
            bool flag;
            for (int i = 2; i <= 100000; i++)
            {
                //先假定为真
                flag = true;
                for (int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        //假的话直接跳出循环
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                {
                    Console.Write(i);
                    for (int l = 0; l < 6-i.ToString().Length; l++)
                    {
                        Console.Write(" ");
                    }
                    k++;
                    if (k % 10 == 0)
                    {
                        Console.WriteLine("");
                    }    
                }
            }
            Console.WriteLine("");
            //两种计算总数的方法
            //Console.WriteLine("质数的总个数：{0}", k);
            Console.WriteLine($"质数的总个数：{k}");
            //结束时间
            DateTime endTime = DateTime.Now;
            //时间间隔
            TimeSpan spantime = endTime - starttime;
            Console.WriteLine($"质数的总个数：{k},共耗时{spantime.TotalSeconds}");
            Console.ReadKey();
        }
        
    }
}

